package memorphic.slide.plugin.wizards;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (slide).
 */

public class ConvertToSlide extends WizardPage {
	private Text projPackage;
	private Text projectName;
	private Text classPrefix;
//	private Text applicationClass;
	
	private ISelection selection;

	/**
	 * Constructor for GenerateSlideWizardPage.
	 * 
	 * @param selection
	 */
	public ConvertToSlide(ISelection selection) {
		super("wizardPage");
		setTitle("Define Slide project structure");
		setDescription("This wizard generates a slide framework for a given flex project.");
		this.selection = selection;
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;
		
		Label label = new Label(container, SWT.NULL);
		label.setText("&Project:");
		projectName = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		projectName.setLayoutData(gd);
		projectName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
//		Button browseButton = new Button(container, SWT.PUSH);
//		browseButton.setText("&Browse...");
//		browseButton.addSelectionListener(new SelectionAdapter() {
//			public void browseProjects(SelectionEvent e) {
//				
//			}
//		});
		
		label = new Label(container, SWT.NULL);
		label.setText("&Package:");
		
		projPackage = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		projPackage.setLayoutData(gd);
		projPackage.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		label = new Label(container, SWT.NULL);
		label.setText("&Prefix:");

		classPrefix = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		classPrefix.setLayoutData(gd);
		classPrefix.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
//		label = new Label(container, SWT.NULL);
//		label.setText("&Application:");
//		
//		applicationClass = new Text(container, SWT.BORDER | SWT.SINGLE);
//		gd = new GridData(GridData.FILL_HORIZONTAL);
//		applicationClass.setLayoutData(gd);
//		applicationClass.addModifyListener(new ModifyListener() {
//			public void modifyText(ModifyEvent e) {
//				dialogChanged();
//			}
//		});
		
		initialize();
		dialogChanged();
		setControl(container);
	}

	/**
	 * Tests if the current workbench selection is a suitable container to use.
	 */

	private void initialize() {
		if (selection != null && selection.isEmpty() == false
				&& selection instanceof IStructuredSelection) {
			IStructuredSelection ssel = (IStructuredSelection) selection;
			if (ssel.size() > 1)
				return;
			Object obj = ssel.getFirstElement();
			if (obj instanceof IResource) {
				IContainer container;
				if (obj instanceof IContainer)
					container = (IContainer) obj;
				else
					container = ((IResource) obj).getParent();
				String nameStr = container.getName().toString();
				projectName.setText(nameStr);
				nameStr = nameStr.replaceFirst(nameStr.substring(0, 1), nameStr.substring(0, 1).toUpperCase());
				classPrefix.setText(nameStr);
			}
		}
	}
	
	/**
	 * Ensures that both text fields are set.
	 */

	private void dialogChanged() {
		String projPackage = getProjPackage();
		String classPrefix = getClassPrefix();
//		String applicationClass = getApplictionClass();

		if (getProjectName().length() == 0) {
			updateStatus("Project must be specified");
			return;
		}
		else {
			IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
			Boolean valid = false;
			for (int i = 0; i < projects.length; i++) {
				IProject project = projects[i];
				if (project.getName().equals(getProjectName())) {
					valid = true;
					break;
				}
			}
			if (!valid) {
				updateStatus("Project must match existing project");
				return;
			}
		}
		if (projPackage.length() == 0) {
			updateStatus("Package must be specified");
			return;
		}
		
		if (classPrefix.length() == 0) {
			updateStatus("Class prefix must be specified");
			return;
		}
		
//		if (applicationClass.length() == 0) {
//			updateStatus("Application class must be specified");
//			return;
//		}
		
		updateStatus(null);
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	
//	private void browseProjects() {
//		
//	}
	
	public String getProjectName() {
		return projectName.getText();
	}
	
	public String getProjPackage() {
		return projPackage.getText();
	}

	public String getProjectDirectory(String projName) {
		IProject[] projs = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		for (int i = 0; i < projs.length; i++)
		{
			IProject proj = projs[i];
			if (proj.getName().equals(projName))
			{
				return proj.getLocation().toString();
			}
		}
		return null;
	}
	
	public String getClassPrefix() {
		return classPrefix.getText();
	}
	
//	public String getApplictionClass() {
//		return applicatoinClass.getText();
//	}
}